package com.xiaobai.common.utils.config.page;


import com.github.pagehelper.PageHelper;
import com.xiaobai.common.utils.tool.StringUtil;
import jakarta.servlet.http.HttpServletRequest;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

@Aspect
@Component
public class PageAspect {
    private static final Logger log = LoggerFactory.getLogger(PageAspect.class);

    public PageAspect() {
    }

    @Around("@annotation(pager)")
    public Object doAfterReturning(ProceedingJoinPoint joinPoint, Pager pager) throws Throwable {
        Object var8;
        try {
            HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
            String pageNo = request.getParameter(pager.page());
            String pageSize = request.getParameter(pager.pageSize());
            if (StringUtil.isBlank(pageNo)) {
                pageNo = "1";
            }
   //这个方法判断字符串是否为空
            if (StringUtil.isBlank(pageSize)) {
                pageSize = "10";
            }
            int page = Integer.parseInt(pageNo);
            if (page <= 0) {
                page = Integer.parseInt("1");
            }
            int size = Integer.parseInt(pageSize);
            if (size <= 0 || size > 1000) {
                size = Integer.parseInt("10");
            }
   //调用分页插件当中的开启分页
            PageHelper.startPage(page, size);
            var8 = joinPoint.proceed();
        } catch (Throwable var12) {
            Throwable e = var12;
            log.error("自动分页开启失败！", e);
            throw e;
        } finally {
            PageHelper.clearPage();
        }

        return var8;
    }
}
